package com.sptpc.learnspring;

import com.sptpc.learnspring.common.OrderState;
import com.sptpc.learnspring.model.Category;
import com.sptpc.learnspring.model.Drink;
import com.sptpc.learnspring.model.Order;
import com.sptpc.learnspring.service.CategoryService;
import com.sptpc.learnspring.service.DrinkService;
import com.sptpc.learnspring.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;

@Slf4j
@SpringBootApplication
public class LearnSpringApplication implements ApplicationRunner {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DrinkService drinkService;
    @Autowired
    private OrderService OrderService;

    public static void main(String[] args) {
        SpringApplication.run(LearnSpringApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //模拟在分类下新建饮品
        Category search = categoryService.findOne("咖啡");
        Drink latte = drinkService.saveDrink(
                Drink.builder().name("拿铁")
                        .price(Money.of(CurrencyUnit.of("CNY"), 13.5)) // 创建Money类型
                        .category(search) // 这行代码是维护一对多关系的关键
                        .build()
        );
        log.info("创建一个新的饮品:{}", latte);
    }

    private void orderCRUD(){
        //模拟创建订单
        Drink drink1 = drinkService.getDrink("西瓜汁");
        Drink drink2 = drinkService.getDrink("柠檬汁");
        Order order = OrderService.saveOrder("小黄", drink1,drink2);
        log.info("订单创建成功:{}", order);

        //模拟通过用户名查看订单
        List<Order> orders = OrderService.getOrders("小黄");
        orders.forEach(o -> log.info("订单信息:{}", o));

        //模拟更改订单，取消某个饮品
        Drink deleteDrink = drinkService.getDrink("西瓜汁");
        Order updateOrder = OrderService.updateOrder(5L, deleteDrink);
        log.info("更新后的订单:{}", updateOrder);

        //取消订单，不是真正删除订单，
        //在数据表中设置一个逻辑删除字段控制字段是否被查询出来
        OrderService.delete(5L);
    }
    private void drinkCRUD() {
        //模拟在分类下新建饮品
        Category search = categoryService.findOne("果汁");
        Drink watermelon = drinkService.saveDrink(
           Drink.builder().name("柠檬汁")
                .price(Money.of(CurrencyUnit.of("CNY"), 8.0)) // 创建Money类型
                   .category(search) // 这行代码是维护一对多关系的关键
                .build()
        );
        log.info("创建一个新的饮品:{}", watermelon);

        //模拟查询在分类下所有饮品
        Category search2 = categoryService.findOne("果汁");
        List<Drink> drinks = drinkService.getAllDrinkes(search2.getId());
        drinks.forEach(drink -> log.info("包含饮品:{}",drink));

        //模拟修改饮品的分类
        Category newCategory = categoryService.findOne("咖啡");
        Drink oldDrink = drinkService.getDrink("西瓜汁");
        oldDrink.setCategory(newCategory);
        Drink updatedDrink = drinkService.updateDrink(oldDrink);
        log.info("更新了饮品:{}", updatedDrink);

        //模拟删除了指定的饮品
        drinkService.delete(drinkService.getDrink("西瓜汁"));
    }
    private void categoryCRUD(){
        //创建新分类
        Category category = categoryService.saveCategory("果汁");
        log.info("创建一个新的分类:{}",category);

        //查看所有分类
        List<Category> categoryList = categoryService.findAll();
        categoryList.forEach(c -> {
            log.info("分类:{}", c);
        });

        //搜索某个分类
        Category serach = categoryService.findOne("咖%");
        log.info("查找分类:{}", serach);

        //更新分类
        Category update = categoryService.update(1L,"鲜榨果汁");
        log.info("更新分类:{}", update);

        //删除分类
        if (categoryService.delete(1)){
            log.info("删除成功");
        }else {
            log.error("删除失败");
        }
    }
}
